package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.repository.DataAccess.DAO_Album;
import de.fhb.itunes.repository.DataAccess.DAO_Episode;
import de.fhb.itunes.repository.DataAccess.DAO_Film;
import de.fhb.itunes.repository.DataAccess.DAO_Musik;
import de.fhb.itunes.repository.DataAccess.DAO_TvSendung;
import de.fhb.itunes.repository.data.Film_VO;

/**
 * Servlet implementation class filme
 */
@WebServlet("/filme")
public class filme extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public filme() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				// logik anfang
				Enumeration en = request.getParameterNames();
				int modus = -1;
				while (en.hasMoreElements()) {
					String paramName = (String) en.nextElement();
					String paraValue = request.getParameter(paramName);
					if (paramName.contains("mode"))
						modus = Integer.parseInt(paraValue);
				}
				if (modus != -1) {
					Object objfilm = session.getAttribute("film");
					if(objfilm instanceof DAO_Film){
						DAO_Film filmeverwaltung = (DAO_Film) objfilm;
						List<Film_VO> filmListe = null;
						switch (modus) {
						case 1:
							filmListe = filmeverwaltung.getAll();
							request.setAttribute("filmListe", filmListe);
							break;
						case 2:
							request.setAttribute("isSuche", true);
							break;
						case 3:
							filmListe = filmeverwaltung.getLastAdded(20);
							request.setAttribute("filmListe", filmListe);
							break;
						case 4:
							filmListe = filmeverwaltung.getAllHD();
							request.setAttribute("filmListe", filmListe);
							break;
						case 5:
							filmListe = filmeverwaltung.getAllSD();
							request.setAttribute("filmListe", filmListe);
							break;
						default:
							
							break;
						}
						nextJSP = "/Film.jsp";
					} 
					
				} else {
					// kein Mode
					nextJSP = "/Film.jsp";
				}
				
				// logik ende
			}
		} else
			nextJSP = "/login.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				// logik anfang
				nextJSP = "/Film.jsp";
				Enumeration en = request.getParameterNames();
				String keyword = "";
				while (en.hasMoreElements()) {
					String paramName = (String) en.nextElement();
					String paraValue = request.getParameter(paramName);
					if (paramName.contains("keyword")) {
						keyword = paraValue;
					}
					if (keyword != ""){
						Object objfilm = session.getAttribute("film");
						if(objfilm instanceof DAO_Film){
							DAO_Film filmeverwaltung = (DAO_Film) objfilm;
							List<Film_VO> filmListe = filmeverwaltung.searchByName(keyword);
							request.setAttribute("filmListe", filmListe); 
							
						}
					}
				}
				// logik ende
			}
		} else
			nextJSP = "/login.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

}
